<template>
	<!-- 电池添加 -->
	<div>
		<div class="edit-top">
			电池添加
			<i class="el-icon-close" @click='cancel'></i>
		</div>
		<div class="edit-content">
			<!-- <div class="edit-title">电池信息</div> -->
			<div class="edit-item">
				<label>电池编码：</label><input v-model="info.code" type="text" />
			</div>
			<div class="edit-item">
				<label>电池型号：</label><input v-model="info.batteryModel" type="text" />
			</div>
			<div class="edit-item">
				<label>电池类型：</label><input v-model="info.batteryType" type="text" />
			</div>
			<div class="edit-item">
				<label>入库换电站：</label>
				<select v-model="info.stationId">
				   <option v-for="item in station_list" :value="item.id" >{{item.name}}</option>
				</select>
			</div>
			<div class="edit-item" v-if="info.stationId != 0">
				<label>货舱编号：</label><input v-model="info.WarehouseNumber" type="text" />
			</div>
		</div>
		<div class="edit-bottom">
			<span @click='cancel'>取消</span>
			<el-button @click='save' type="primary">保存</el-button>
		</div>
	</div>
</template>

<script>
	import Global from '@/components/Global.vue'
	export default {
		data() {
			return {
				dict: '',
				station_list:[],
				info: {
					code: '',
					batteryModel: '',
					batteryType: '',
					currentPosition: 0,
					stationId:'',
					WarehouseNumber:''
				}
			}
		},
		mounted() {
			this.get_dict();
			this.get_station_list();
		},
		methods: {
			get_station_list() {
				var self = this;
				this.$axios.post(this.$global.serverIp + 'station/station/findPage', {
						"limit": "10000",
						"page": "1"
					})
					.then(function(res) {
						if (res != '') {
							self.station_list = res.data.list;
							self.station_list.splice(0, 0, { 'id': '', 'name': '不选择' })
							self.info.stationId = self.station_list[0].id;
						}
				
					})
			},
			get_dict() {
				var self = this;
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=battery')
				  .then(function (res) {
					  if(res != '')
						self.dict = res.data;
				  })
			},
			save() {
				if (this.info.code == "") {
					this.$popup.open('电池编码不能为空');
					return;
				}
				if (this.info.batteryType == "") {
					this.$popup.open('电池类型不能为空');
					return;
				}
				if (this.info.batteryModel == "") {
					this.$popup.open('电池型号不能为空');
					return;
				}
				var self = this;
				this.$axios.post(this.$global.serverIp + 'station/battery/save', {
					"code":self.info.code,
					"model":self.info.batteryModel,
					"type":self.info.batteryType,
					"stationId":self.info.stationId,
					"batteryCabinCode":self.info.WarehouseNumber
				})
					.then(function(res) {
						if (res != '') {
							self.success('添加成功');
						}
					})
			},
			success(content) {
				var self = this;
				this.$alert(content, '提示', {
					confirmButtonText: '确定',
					callback: action => {
						self.$emit("saveAdd", 2);
					}
				});
			},
			
			cancel() {
				this.$emit("saveAdd", 1);
			}
		}
	}
</script>

